import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:getx_example/common/my_base_controller.dart';
import 'package:getx_example/get_connect/controller/my_get_connect_controller.dart';
import 'package:getx_example/get_connect/model/my_get_connect_model.dart';

class MyGetContentPage extends GetView<MyGetConnectController> {
  static const String route = "/MyGetContentPage";

  const MyGetContentPage({super.key});

  _buildListView(MyGetConnectModel? state) {
    return ListView.separated(
      itemCount: 10,
      itemBuilder: (context, index) {
        return ListTile(
          onTap: () {},
          title: Text(state?.body ?? ""),
          subtitle: Text("内容 ${state?.id}"),
        );
      },
      separatorBuilder: (BuildContext context, int index) {
        return const Divider();
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("GetConnect Page"),
      ),
      body:
      GetX<MyGetConnectController>(initState: (state) {
        controller.getData();
      }, init: controller,
      builder: ((_) {

        debugPrint("controller.viewState.value: ${controller.viewState.value}");

        switch (controller.viewState.value) {
          case ViewState.loading:
            return const Center(
              child: CircularProgressIndicator(),
            );
          default:
           return _buildListView(controller.getConnectModel);
        }
      })),

      floatingActionButton: FloatingActionButton(
        onPressed: () {
          controller.getData();
        },
        child: const Icon(Icons.add),
      ),

      // controller.obx(
      //   (state) => _buildListView(state),
      //   onEmpty: const Text("onEmpty"),
      //   onLoading: Center(
      //     child: Column(
      //       children: [
      //         const Text("没有数据"),

      //       ],
      //     ),
      //   ),
      //   onError: (err) => Text("onEmpty" + err.toString()),
      // ),
    );
  }
}
